package com.my.screens;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.my.drinkinggame.Main;
import com.my.drinkinggame.Renderer;
import com.my.drinkinggame.StaticFonts;
import com.my.drinkinggame.StaticSprites;

public class AddedCards extends Screen{
	boolean available;
	Main main;
	public OrthographicCamera cam;
	public int selectedItem;
	public int amountofItems;
	public int startPrinting;
	Vector3 translationVector;
	public File currentDirectory;
	public File currentFile;
	public Sprite activeOpen;
	public Sprite activeCancel;
	public Sprite activeUp;
	public Sprite activeDown;
	public Sprite marker;
	public Sprite headerSprite;
	public Sprite upPointer;
	public Sprite downPointer;
	public boolean resetButtons, upArrowPressed, downArrowPressed;
	public ArrayList<File> files;
	public ArrayList<String> fileNames;
	public float xPos;
	public float yPos;
	public float timer;

	public AddedCards(Main m) {
		cam = new OrthographicCamera();
		translationVector = new Vector3();
		main = m;
		xPos = 0;
		yPos = 0;
		timer = 0;
		activeOpen = StaticSprites.openBut;
		activeCancel = StaticSprites.cancelBut;
		activeUp = StaticSprites.upArrow;
		activeDown = StaticSprites.downArrow;
		marker = StaticSprites.marker;
		headerSprite = StaticSprites.addedCardsBut;
		upPointer = StaticSprites.upPointer;
		downPointer = StaticSprites.downPointer;
		upPointer.setScale(0.7f);
		downPointer.setScale(0.7f);
		marker.setScale(0.1f);
		selectedItem = 0;
		startPrinting = 0;
		resetButtons = false;
		upArrowPressed = false;
		downArrowPressed = false;
		files = new ArrayList<File>();
		fileNames = new ArrayList<String>();
		if (Gdx.files.isExternalStorageAvailable()) {
			if (Gdx.files.external("HHObeergame").exists() && Gdx.files.external("HHObeergame").isDirectory()) {
				available = true;
				currentDirectory = Gdx.files.external("HHObeergame").file();
				amountofItems = currentDirectory.listFiles().length-1;
			} else {
				Gdx.files.external("HHObeergame").file().mkdir();
				available = true;
				currentDirectory = Gdx.files.external("HHObeergame").file();
				amountofItems = currentDirectory.listFiles().length;
			}
		} else {
			available = false;
		}
	}

	public void resetAll(){
		selectedItem = 0;
		startPrinting = 0;
		upPointer.setScale(0.7f);
		downPointer.setScale(0.7f);
		resetButtons = true;
		upArrowPressed = false;
		downArrowPressed = false;
		files.clear();
		fileNames.clear();
	}

	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			resetAll();
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		if(activeUp.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(selectedItem == 0){
				if(startPrinting > 0){
					upPointer.setScale(0.85f);
				}
			}
			upArrowPressed = true;
		}
		if(activeDown.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(selectedItem == 14 && files.size() > 15){
				if((startPrinting + 14) < files.size()-1){
					downPointer.setScale(0.85f);
				}
			}
			downArrowPressed = true;
		}
		resetButtons = false;
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		if(activeUp.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(selectedItem > 0){
				selectedItem--;
			}
			else if(selectedItem == 0){
				if(startPrinting > 0){
					startPrinting--;
				}
			}
		}
		if(activeDown.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(selectedItem < files.size()-1 && files.size() > 0){
				if(selectedItem < 14){
					selectedItem++;
				}
				else if(selectedItem == 14 && files.size() > 15){
					if((startPrinting + 14) < files.size()-1){
						startPrinting++;
					}
				}
			}
		}
		if(activeOpen.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(files.size() > 0){
				String externalFile = (new FileHandle(files.get(selectedItem+startPrinting)).readString());
				((CreateCard) main.getState(Main.SCREEN_CREATECARD)).setHC(externalFile);
				resetAll();
				main.setScreen(Main.SCREEN_CREATECARD);
			}
		}
		if(activeCancel.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			resetAll();
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		upPointer.setScale(0.7f);
		downPointer.setScale(0.7f);
		upArrowPressed = false;
		downArrowPressed = false;
		resetButtons = true;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		return false;
	}

	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	public void updateOpenBut(){
		if(activeOpen.getBoundingRectangle().contains(xPos, yPos)){
			activeOpen = StaticSprites.openButSel;
		}
		else{
			activeOpen = StaticSprites.openBut;
		}
	}

	public void updateCancelBut(){
		if(activeCancel.getBoundingRectangle().contains(xPos, yPos)){
			activeCancel = StaticSprites.cancelButSel;
		}
		else{
			activeCancel = StaticSprites.cancelBut;
		}
	}

	public void updateUpBut(){
		if(activeUp.getBoundingRectangle().contains(xPos, yPos)){
			activeUp = StaticSprites.upArrowSel;
		}
		else{
			activeUp = StaticSprites.upArrow;
		}
	}

	public void updateDownBut(){
		if(activeDown.getBoundingRectangle().contains(xPos, yPos)){
			activeDown = StaticSprites.downArrowSel;
		}
		else{
			activeDown = StaticSprites.downArrow;
		}
	}

	public void resetButs(){
		activeCancel = StaticSprites.cancelBut;
		activeOpen = StaticSprites.openBut;
		activeUp = StaticSprites.upArrow;
		activeDown = StaticSprites.downArrow;
	}

	@Override
	public void update() {
		updateOpenBut();
		updateCancelBut();
		updateUpBut();
		updateDownBut();
		if(resetButtons){
			resetButs();
		}

	}

	@Override
	public void render() {
		Renderer.getInstance().renderAddedCards(this);

	}

	public void renderAddedCards(SpriteBatch sb){
		StaticSprites.addedCardsBG.draw(sb);
		StaticFonts.comic_48.setScale(1);
		int index = 1;
		if(files.size() > 15){
			for (int i = 0; i < 15; i++) {
				marker.setPosition(StaticSprites.cardBGSprite.getX()-StaticFonts.comic_48.getBounds("  ").width, 
						cam.position.y+Gdx.graphics.getHeight()/2.6f-
						StaticSprites.marker.getHeight()/1.13f-
						StaticFonts.comic_48.getBounds("").height*1.5f*selectedItem);
				StaticFonts.comic_48.draw(sb, fileNames.get(startPrinting+i), 
						StaticSprites.cardBGSprite.getX()+StaticFonts.comic_48.getBounds("      ").width, 
						cam.position.y+Gdx.graphics.getHeight()/2.6f-StaticFonts.comic_48.getBounds("").height*index*1.5f);
				index++;
			}			
			if(startPrinting != files.size()-15){
				downPointer.setPosition(StaticSprites.cardBGSprite.getX()+
						StaticSprites.downPointer.getWidth()/1.2f, 
						StaticSprites.cardBGSprite.getY()+StaticSprites.cardBGSprite.getHeight()/2.8f);
				downPointer.draw(sb);
			}
			if(startPrinting != 0){
				upPointer.setPosition(StaticSprites.cardBGSprite.getX()+
						StaticSprites.downPointer.getWidth()/1.2f, 
						StaticSprites.cardBGSprite.getY()+StaticSprites.cardBGSprite.getHeight()/1.17f);
				upPointer.draw(sb);
			}
		}
		else{
			for (int i = 0; i < files.size(); i++) {
				marker.setPosition(StaticSprites.cardBGSprite.getX()-StaticFonts.comic_48.getBounds("   ").width, 
						cam.position.y+Gdx.graphics.getHeight()/2.6f-
						StaticSprites.marker.getHeight()/1.32f-
						StaticFonts.comic_48.getBounds("").height*1.5f*selectedItem);
				StaticFonts.comic_48.draw(sb, fileNames.get(i), 
						StaticSprites.cardBGSprite.getX()+StaticFonts.comic_48.getBounds("     ").width, 
						cam.position.y+Gdx.graphics.getHeight()/2.5f-StaticFonts.comic_48.getBounds("").height*index*1.5f);
				index++;
			}
		}
		if(amountofItems > 0){
			marker.draw(sb);
		}
		headerSprite.setPosition(StaticSprites.cardBGSprite.getX()+
				StaticSprites.addedCardsBut.getWidth()/2, 
				StaticSprites.cardBGSprite.getY()+StaticSprites.cardBGSprite.getHeight()/1.1f);
		headerSprite.draw(sb);
		activeUp.setPosition(cam.position.x-StaticSprites.upArrow.getWidth()/2+StaticSprites.cancelBut.getWidth(), 
				cam.position.y-Gdx.graphics.getHeight()/3f);
		activeUp.draw(sb);
		activeDown.setPosition(cam.position.x-StaticSprites.downArrow.getWidth()/2+StaticSprites.cancelBut.getWidth(), 
				cam.position.y-Gdx.graphics.getHeight()/3f-StaticSprites.upArrow.getHeight());
		activeDown.draw(sb);
		activeOpen.setPosition(cam.position.x-StaticSprites.saveBut.getWidth()/2-StaticSprites.cancelBut.getWidth(), 
				cam.position.y-Gdx.graphics.getHeight()/2.3f-12);
		activeOpen.draw(sb);
		activeCancel.setPosition(cam.position.x-StaticSprites.saveBut.getWidth()/2, 
				cam.position.y-Gdx.graphics.getHeight()/2.3f);
		activeCancel.draw(sb);
	}

	@Override
	public void enter() {
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
		files.clear();
		fileNames.clear();
		if(available){
			for(File f : currentDirectory.listFiles()){
				files.add(f);
				fileNames.add(f.getName().substring(0, f.getName().length()-4));
			}
		}
		amountofItems = currentDirectory.listFiles().length;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

}
